package ylesanded.kimpExample;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Kimp extends JFrame {
		
	public ShapeList shapeList = new ShapeList();
	
	public boolean selectMode = false;
	public Point lastPoint = null;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Kimp k = new Kimp();
	}
	
	public Kimp() {
		this.setTitle("Blah");
		this.setSize(800, 600);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mousePressed(e);
				
				if (selectMode) {
					shapeList.newState();
					lastPoint = e.getPoint();
				} else {
					if (SwingUtilities.isLeftMouseButton(e)) {
						// draw new stuff
						Rectangle r = new Rectangle(e.getPoint());
						r.end = e.getPoint();
						shapeList.addShape(r);
					} else {
						Line l = new Line(e.getPoint());
						shapeList.addShape(l);
					}
					repaint();
				}
			}
		});
		
		this.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseDragged(MouseEvent e) {
				super.mouseDragged(e);
				
				if (selectMode) {
					Shape s = shapeList.getSelectedShape();
					if (s != null) {
						System.out.println("jee");
						s.move(e.getPoint().x - lastPoint.x, e.getPoint().y - lastPoint.y);
						repaint();
					}
					lastPoint = e.getPoint();
				} else {
					Shape s = shapeList.getLastShape();
					if (s != null) {
						s.draw(e.getPoint());
						repaint();
					}
				}
			}
			
			@Override
			public void mouseMoved(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mouseMoved(e);
				
				if (selectMode) {
					shapeList.select(e.getPoint());
					repaint();
				}
			}
		});
		
		this.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				super.keyPressed(e);
				
				if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
					boolean undone = shapeList.undo();
					if (!undone) {
						System.out.println("EXIT?");
					}
					repaint();
				}
				
				if (e.getKeyCode() == KeyEvent.VK_SPACE) {
					shapeList.redo();
					repaint();
				}
				
				if (e.getKeyCode() == KeyEvent.VK_CONTROL) {
					selectMode = true;
				}
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				super.keyReleased(e);
				if (e.getKeyCode() == KeyEvent.VK_CONTROL) {
					selectMode = false;
					shapeList.unselectAll();
				}
			}
		});
	}
	
	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		super.paint(g);
		
		for (Shape s : shapeList.getShapes()) {
			s.paint(g);
		}
	}

}